'use strict';

/**
 * @name 需求list页的controller
 * @author gu
 */

angular.module('metalabPartnerApp').controller('DemandListCtrl', ['$scope', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaSEO', '$state', 'MetaConstants',
    function ($scope, $stateParams, resource, MetaRemind, MetaSEO, $state, MetaConstants) {
        $scope.myInterval = MetaConstants.slideTime;

        $scope.tabs = [
            {
                name: '所有需求',
                active: false,
                select: function () {
                    $scope.searchText = $scope.searchText || '';
                    $state.go('meta.demand.list.all');
                }
            }, {
                name: '我发布的需求',
                active: false,
                select: function () {
                    $scope.searchText = '';
                    $state.go('meta.demand.list.released');
                }
            }, {
                name: '我收藏的需求',
                active: false,
                select: function () {
                    $scope.searchText = '';
                    $state.go('meta.demand.list.favorite');
                }
            }
        ];

        ///--初始化
        $scope.filters = [{//筛选配置
            title: '应用领域',
            items: ['化学化工', '能源环保', '生农医药', '机械电子', '材料科学', '其他'],
            config: {
                chose: '不限',
                showNumber: 0
            }
        }];

        /**
         * @function 标签选择
         * @param item 选中的标签
         */
        $scope.selectTag = function (item) {
            $scope.searchText = item;
            $scope.tagChose = $scope.tagChose === item ? null : item;
            $state.go("meta.demand.list.all", {
                page: 1,
                area: $scope.filters[0].config.chose == '不限' ? null : $scope.filters[0].config.chose,
                keywords: $scope.tagChose
            });
        };

        /**
         * @function 监听筛选
         */
        $scope.$watch("filters[0].config.chose", function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $state.go($state.current.name, {
                    page: '1',
                    area: newVal === '不限' ? null : newVal,
                    keywords: $scope.searchText
                });
            }
        });

        /**
         * @function 搜索功能
         */
        $scope.search = function () {
            // if ($scope.searchText && $scope.searchText.length > 0) {
            $state.go("meta.demand.list.all", {
                page: 1,
                area: $stateParams.area,
                keywords: $scope.searchText
            });
            // } else {
            //     MetaRemind.warn('请输入关键字进行搜索');
            // }
        };
    }]);
